package com.chauncy.DMS.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import util.List2Json1.List2Json1;

import com.chauncy.DMS.bean.ManagerInfoBean;
import com.chauncy.DMS.dao.ManagerDaoImplement;

public class AddHealthInfoAndLinkageServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//获取楼栋号
		List<ManagerInfoBean> managerList= (List)request.getSession().getAttribute("managerInfoList");
		String buildings = managerList.get(0).getManager_buildings();
		ManagerDaoImplement daoImplement = new ManagerDaoImplement();
		//联动菜单 查询本楼层是否有人居住
		String sql = "select Dorm_number from dorm_info_table" +
				" WHERE Actual_number_of_people > 0 AND" +
				" Buildings_number = ? AND Floor_number = ?;";
		int floorNub = Integer.parseInt(String.valueOf(request.getParameter("opValue")));
		if( floorNub != 0 && buildings != null ){
			Object[] param = {buildings,floorNub};
			List<Map<String,Object>> dormNubList = daoImplement.query(sql, param);
			if(dormNubList.size() == 0){
				response.getWriter().print("null");
			}else{
				JSONArray dormNubArray = JSONArray.fromObject( dormNubList);
				response.getWriter().print(dormNubArray);
			}
		}else{
			System.out.println("list is null");
			response.getWriter().print("nulls");
		}
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ManagerDaoImplement daoImplement = new ManagerDaoImplement();
		//获取楼号
		List<ManagerInfoBean> managerList= (List)request.getSession().getAttribute("managerInfoList");
		String buildings = managerList.get(0).getManager_buildings();
		System.out.println(buildings);
		String dorm_number = null;
		int t_value= 0;
		String floor_number= null;
		String deduction = null;
		String health_type= null;
		String health_grade= null;
		String remarks = null;
		int flag = 0;
		//sql
		String sql = "INSERT INTO " +
				"health_info_table" +
				" (health_info_table.Dorm_number," +
				"health_info_table.Deduction," +
				"health_info_table.The_Value," +
				"health_info_table.Buildings_number," +
				"health_info_table.Floor_number," +
				"health_info_table.Grade," +
				"health_info_table.health_type," +
				"health_info_table.Att_time," +
				"health_info_table.Remarks) VALUES (?,?,?,?,?,?,?,NOW(),? );";
		JSONObject object = JSONObject.fromObject(request.getParameter("obj"));
		if(object != null && !object.equals("")){
			System.out.println(object);
			//宿舍编号
			dorm_number =String.valueOf( object.get("dorm_number"));
			//楼层号
			floor_number = String.valueOf(object.get("floor_number"));
			//t_value
			t_value =Integer.parseInt(String.valueOf( object.get("t_value")));
			//扣分详情
			deduction =String.valueOf( object.get("deduction"));
			//卫生类型
			health_type =String.valueOf( object.get("health_type"));
			//卫生等级
			health_grade =String.valueOf( object.get("health_grade"));
			//remarks
			remarks =String.valueOf( object.get("remarks"));
			//缺省值
			Object[] param = {dorm_number,deduction,t_value,buildings,floor_number,health_grade,health_type,remarks};
			//插入数据
			flag = daoImplement.updata(sql, param);
			response.getWriter().print(flag);
		}
		
	}

}
